package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.toast.ToastUtils;
import com.qiantu.phone.R;
import com.qiantu.api.entity.ControlDataAirConditionerBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.phone.ui.adapter.DeviceModeLabelAdapter;
import com.qiantu.phone.ui.adapter.DeviceModeValueAdapter;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.widget.AirConditionerSeekBar;

import java.util.ArrayList;
import java.util.List;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.MODE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_OFF;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_ON;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.TEMPERATURE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.WIND_DIRECTION;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.WIND_SPEED;

/**
 * @Author luzx
 * @Date 2022/1/10
 */
public class AirConditionerShotView extends BaseDeviceActView {

    private AirConditionerSeekBar airConditionerSeekBar;
    private RecyclerView modeRecyclerView;
    private RecyclerView modeValueRecyclerView;
    private DeviceModeLabelAdapter mDeviceModeLabelAdapter;
    private DeviceModeValueAdapter mDeviceModeValueAdapter;
    private ControlDataAirConditionerBean mAirConditionerInfo;
    private ControlDataAirConditionerBean.ControlData controlData;
    private String selectedLabel;
    private String mode;
    private String windSpeed;
    private String windDirection;

    public AirConditionerShotView(Context context, String type, String deviceType) {
        super(context, type, deviceType);
    }

    @Override
    public int getLayoutId() {
        return R.layout.air_conditioner_shot_view;
    }

    @Override
    public void initView() {
        airConditionerSeekBar = findViewById(R.id.air_conditioner_seek_bar);
        modeRecyclerView = findViewById(R.id.mode_recycler_view);
        modeValueRecyclerView = findViewById(R.id.mode_value_recycler_view);
        airConditionerSeekBar.setOnChangeListener(new AirConditionerSeekBar.OnChangeListener() {

            @Override
            public void onValueChanged(float value) {

            }

            @Override
            public void onStopTrackingTouch(float value) {
                addAct(POWER, POWER_ON);
                addAct(TEMPERATURE, getTempText(value));
            }

            @Override
            public void onSwitch(boolean isOpen) {
                if(isOpen){
                    addAct(POWER, POWER_ON);
                    addAct(TEMPERATURE, getTempText(airConditionerSeekBar.getTemp()));
                }else{
                    addAct(POWER, POWER_OFF);
                    removeAct(TEMPERATURE);
                }
                removeAct(MODE);
                removeAct(WIND_SPEED);
                removeAct(WIND_DIRECTION);
                mode = defaultValue;
                windSpeed =  defaultValue;
                windDirection =  defaultValue;
                setValueByLabel();
            }
        });
        List<String> modes = new ArrayList<>();
        mAirConditionerInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataAirConditionerBean.class);
        controlData = mAirConditionerInfo.getControlData();
        airConditionerSeekBar.setMinMax(controlData.getTemperature_min(), controlData.getTemperature_max());
        if (controlData.getMode() != null && controlData.getMode().size() > 0) {
            modes.add(getResources().getString(R.string.mode));
        }
        if (controlData.getWindspeed() != null && controlData.getWindspeed(). size() > 0) {
            modes.add(getResources().getString(R.string.wind_speed));
        }
        if (controlData.getWinddirection() != null && controlData.getWinddirection().size() > 0) {
            modes.add(getResources().getString(R.string.wind_direction));
        }
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), modes.size());
        modeRecyclerView.setLayoutManager(gridLayoutManager);
        mDeviceModeLabelAdapter = new DeviceModeLabelAdapter(getContext());
        mDeviceModeLabelAdapter.setData(modes);
        mDeviceModeLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                selectedLabel = mDeviceModeLabelAdapter.getData().get(position);
                mDeviceModeLabelAdapter.selectItem(position);
                if (selectedLabel.equals(getResources().getString(R.string.mode))) {
                    mDeviceModeValueAdapter.setData(controlData.getMode());
                    selectValue(true, mode);
                } else if (selectedLabel.equals(getResources().getString(R.string.wind_speed))) {
                    mDeviceModeValueAdapter.setData(controlData.getWindspeed());
                    selectValue(false, windSpeed);
                } else if (selectedLabel.equals(getResources().getString(R.string.wind_direction))) {
                    mDeviceModeValueAdapter.setData(controlData.getWinddirection());
                    selectValue(false, windDirection);
                }
            }
        });
        modeRecyclerView.setAdapter(mDeviceModeLabelAdapter);
        mDeviceModeLabelAdapter.selectItem(0);
        selectedLabel = modes.get(0);
        mDeviceModeValueAdapter = new DeviceModeValueAdapter(getContext());
        mDeviceModeValueAdapter.setData(controlData.getMode());
        mDeviceModeValueAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                if(!airConditionerSeekBar.isOpen()){
                    ToastUtils.show(R.string.device_off_disable);
                    return;
                }
                DeviceModeValue valueBean = mDeviceModeValueAdapter.getData().get(position);
                boolean isMode = false;
                if (selectedLabel.equals(getResources().getString(R.string.mode))) {
                    int nameRes = getResources().getIdentifier(valueBean.getName(), "string", getContext().getPackageName());
                    airConditionerSeekBar.setModeName(getResources().getString(nameRes));
                    mode = valueBean.getValue() + "";
                    isMode = true;
                    addAct(MODE, mode);
                } else if (selectedLabel.equals(getResources().getString(R.string.wind_speed))) {
                    windSpeed = valueBean.getValue() + "";
                    isMode = false;
                    addAct(WIND_SPEED, windSpeed);
                } else if (selectedLabel.equals(getResources().getString(R.string.wind_direction))) {
                    windDirection = valueBean.getValue() + "";
                    isMode = false;
                    addAct(WIND_DIRECTION, windDirection);
                }
                mDeviceModeValueAdapter.selectItem(isMode, position);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        modeValueRecyclerView.setLayoutManager(linearLayoutManager);
        modeValueRecyclerView.setAdapter(mDeviceModeValueAdapter);
    }

    private String getTempText(float temp){
        String text = null;
        if (temp * 10 % 10 == 0) {
            text = (int) temp + "";
        } else {
            text = temp + "";
        }
        return text;
    }

    private void setValueByLabel(){
        if (selectedLabel.equals(getResources().getString(R.string.mode))) {
            selectValue(true, mode);
        } else if (selectedLabel.equals(getResources().getString(R.string.wind_speed))) {
            selectValue(false, windSpeed);
        } else if (selectedLabel.equals(getResources().getString(R.string.wind_direction))) {
            selectValue(false, windDirection);
        }
    }

    private void selectValue(boolean isMode, String value){
        boolean hasSame = false;
        for(int i=0; i<mDeviceModeValueAdapter.getData().size(); i++){
            if(mDeviceModeValueAdapter.getData().get(i).getValue() == Integer.valueOf(value)){
                mDeviceModeValueAdapter.selectItem(isMode, i);
                hasSame = true;
                break;
            }
        }
        if(!hasSame){
            mDeviceModeValueAdapter.clearSelected();
        }
    }

    private String getModeName(String mode){
        for(DeviceModeValue valueBean : controlData.getMode()){
            if(valueBean.getValue() == Integer.valueOf(mode)){
                int nameRes = getResources().getIdentifier(valueBean.getName(), "string", getContext().getPackageName());
                return getResources().getString(nameRes);
            }
        }
        return null;
    }

    @Override
    public void updateView() {
        //默认空的话，添加默认动作开
        if(mDeviceActMap.size() == 0){
            addAct(POWER, POWER_ON);
        }
        //根据已保存动作指令集合更新UI
        airConditionerSeekBar.setOpenState(POWER_ON.equals(getState(POWER,  "0")));
        airConditionerSeekBar.setTemp(Float.valueOf(getState(TEMPERATURE, "0")));
        airConditionerSeekBar.setModeName(getModeName(getState(MODE, defaultValue)));
        mode = getState(MODE, defaultValue);
        windSpeed = getState(WIND_SPEED, defaultValue);
        windDirection = getState(WIND_DIRECTION, defaultValue);
        setValueByLabel();
    }
}
